<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2018/10/23
 * Time: 13:50
 */
namespace app\admin\controller;
use think\Db;
class Words extends Base
{
    /*
     * 敏感词首页
     */
    public function index(){
        if(request()->isAjax ()){
            extract(input());
            $map = [];
            if(isset($key)&&$key!="")
            {
                $map['w_name'] = ['like',"%" . $key . "%"];
            }
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::name('words')->where($map)->count();//计算总页面
            $lists = Db::name('words')
                ->where($map)
                ->page($Nowpage,$limits)
                ->select();
            return json(['code'=>0,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        return $this->fetch('words/index');
    }

    /*
     * 添加敏感词
     */
    public function wordsAdd(){
        if(request ()->isPost ()){
            extract(input());
            $time = date('Y-m-d H:i:s');
            $param = [
                'w_name'=>$title,
                'w_create_time'=>$time,
                'w_update_time'=>$time
            ];
            $res = Db::name('words')->insert($param);
            if($res){
                return json(['code'=>200,'msg'=>'添加成功']);
            }else{
                return json(['code'=>100,'msg'=>'添加失败']);
            }
        }
        return $this->fetch('words/words_add');
    }

    /*
     * 修改敏感词
     */
    public function wordsEdit(){
        if(request ()->isPost ()){
            extract(input());
            $time = date('Y-m-d H:i:s');
            $param = [
                'w_name'=>$title,
                'w_update_time'=>$time
            ];
            $res = Db::name('words')->where('w_id',$id)->update($param);
            if($res){
                return json(['code'=>200,'msg'=>'修改成功']);
            }else{
                return json(['code'=>100,'msg'=>'修改失败']);
            }
        }
        $data = Db::name('words')->where('w_id',input('id'))->find();
        return $this->fetch('words/words_edit',['data'=>$data]);
    }

    /*
     * 删除敏感词
     */
    public function wordsDel(){
        $res = Db::name('words')->delete(input('id'));
        if($res){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }
}